<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="formVisible=true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="sn前五位" value="1"></el-option>
              <el-option label="型号名" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok"/>
      </div>
    </div>
    <el-table stripe style="width: 100%" border fit highlight-current-row ref="tableDataRef" :data="tableData"
              :key="key">
      <el-table-column v-if="colData[0].istrue" min-width="50" align="center" prop="id" label="ID"></el-table-column>
      <el-table-column v-if="colData[1].istrue"  align="center" prop="five_sn" label="SN前五位"></el-table-column>
      <el-table-column v-if="colData[2].istrue"  align="center" prop="model_name" label="型号名"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="100" align="center" prop="distance" label="距离护眼">
        <template slot-scope="scope">
          <p>{{scope.row.distance === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="80" align="center" prop="light" label="光线护眼">
        <template slot-scope="scope">
          <p>{{scope.row.light === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" min-width="100" align="center" prop="reversal" label="反转提醒">
        <template slot-scope="scope">
          <p>{{scope.row.reversal === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue" min-width="100" align="center" prop="shake" label="抖动提醒">
        <template slot-scope="scope">
          <p>{{scope.row.shake === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[7].istrue" min-width="100" align="center" prop="black_white" label="黑白阅读">
        <template slot-scope="scope">
          <p>{{scope.row.black_white === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[8].istrue" min-width="100" align="center" prop="color_temperature" label="色温调节">
        <template slot-scope="scope">
          <p>{{scope.row.color_temperature === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[9].istrue" min-width="100" align="center" prop="tired" label="疲劳提醒">
        <template slot-scope="scope">
          <p>{{scope.row.tired === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[10].istrue" min-width="100" align="center" prop="eye_exercises" label="眼保健操">
        <template slot-scope="scope">
          <p>{{scope.row.eye_exercises === 1 ? '可见' : '隐藏'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[11].istrue" min-width="140" align="center" prop="update_time" label="操作时间">
        <template slot-scope="scope">{{scope.row.update_time| newDate}}</template>
      </el-table-column>
      <el-table-column label="操作" min-width="150" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)">编辑</el-button>
            <el-button size="mini" @click="delEyes(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog title="添加分类" :visible.sync="formVisible" :close-on-click-modal="false" :close-on-press-escape="false"
               width="40%" @close="addFormclose">
      <el-form :model="addForm" ref="ruleForm" :rules="rules" class="diolog_adapt" style="width: 600px">
        <el-form-item label="sn前五位:" label-width="130px" prop="five_sn">
          <el-input v-model="addForm.five_sn" placeholder="请输入sn前五位" :disabled="editSubjects" maxlength="5"></el-input>
        </el-form-item>
        <div class="flex">
          <el-form-item label="距离护眼模块:" label-width="130px" prop="distance">
            <el-switch
              v-model="addForm.distance"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
          <el-form-item label="光感护眼模块:" label-width="130px" prop="light">
            <el-switch
              v-model="addForm.light"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item label="反转提醒模块:" label-width="130px" prop="reversal">
            <el-switch
              v-model="addForm.reversal"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
          <el-form-item label="抖动提醒模块:" label-width="130px" prop="shake">
            <el-switch
              v-model="addForm.shake"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
        </div>

        <div class="flex">
          <el-form-item label="黑白阅读模块:" label-width="130px" prop="black_white">
            <el-switch
              v-model="addForm.black_white"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
          <el-form-item label="色温调节模块:" label-width="130px" prop="color_temperature">
            <el-switch
              v-model="addForm.color_temperature"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
        </div>

        <div class="flex">
          <el-form-item label="疲劳提醒模块:" label-width="130px" prop="tired">
            <el-switch
              v-model="addForm.tired"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
          <el-form-item label="眼保健操模块:" label-width="130px" prop="eye_exercises">
            <el-switch
              v-model="addForm.eye_exercises"
              active-text="可见"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="editContent('ruleForm')">确 定
        </el-button>
        <el-button v-else type="primary" @click="addEyesConfig('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents"
                   :deleteText="deleteDialogs.deleteText"/>
  </div>

</template>

<script>
  import objectable from "@/utils/objectable.js";
  import dynamicTable from "@/components/DynamicTable";
  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js'
  import {addEyesConfig, delEyesConfig, editEyesConfig, queryEyesConfig} from "@/api/equipment";

  export default {
    mixins: [tableMini],
    components: {dynamicTable, deleteDialog},
    data() {
      return {
        key: 1,
        colData: [], //动态控制table
        checkboxVal: ["ID", "SN前五位", "型号名", "距离护眼", "光线护眼", "反转提醒", "抖动提醒", "黑白阅读", "色温调节", "疲劳提醒", "眼保健操", "操作时间"],
        searchTable: "",
        select: "", //搜索条件
        tableData: [],
        listQuery: {
          startSize: 1,
          pageSize: 10,
          userId: localStorage.getItem("ID"),
        },
        addForm: {
          five_sn: '', //sn前5位
          model_name: '',  //产品型号名
          distance: 1,  //距感护眼
          light: false,   //光感护眼
          reversal: false,  //反转提醒
          shake: false,  //抖动提醒
          black_white: false,  //黑白阅读
          color_temperature: false,  //色温调节
          tired: false,  //疲劳提醒
          eye_exercises: false,  //眼保健操
        },
        formVisible: false,
        rules: {
          five_sn: [{required: true, message: '请输入SN前五位', trigger: 'blur'}],
        },
        editSubjects: false,
        deleteDialogs: {show: false, deleteText: '', deleteId: 0},
      }
    },
    created() {
      this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
      this.getEyesConfigTableData()
    },
    methods: {
      Refresh() {
        this.listQuery = {startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), name: undefined}
        this.getEyesConfigTableData()
      },
      //搜索,1:课程名称
      idnameSearch() {
        if (!this.select) return this.$message.warning("请先选择查询条件");
        this.listQuery.startSize = 1
        this.listQuery.pageSize = 10
        switch (this.select) {
          case '1':
            this.listQuery.five_sn = this.searchTable
            this.getEyesConfigTableData();
            break;
          case '2':
            this.listQuery.model_name = this.searchTable
            this.getEyesConfigTableData();
            break;
        }
      },
      openEdit(row) {
        this.formVisible = true
        this.editSubjects = true
        this.addForm = {
          id: row.id,
          five_sn: row.five_sn, //sn前5位
          model_name: row.model_name,  //产品型号名
          distance: row.distance === 1,  //距感护眼
          light: row.light === 1,   //光感护眼
          reversal: row.reversal === 1,  //反转提醒
          shake: row.shake === 1,  //抖动提醒
          black_white: row.black_white === 1,  //黑白阅读
          color_temperature: row.color_temperature === 1,  //色温调节
          tired: row.tired === 1,  //疲劳提醒
          eye_exercises: row.eye_exercises === 1,  //眼保健操
        }
      },
      addEyesConfig(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.userId = localStorage.getItem("ID")
            addEyesConfig(addForm).then(res => {
              this.$message.success(res.msg)
              this.getEyesConfigTableData()
              this.formVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      getEyesConfigTableData() {
        queryEyesConfig(this.listQuery).then(res => {
          if (res.code == 200) {
            this.tableData = res.data
            this.total = res.total
          } else {
            this.$message.error(this.res.msg)
          }
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      editContent(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let editForm = JSON.parse(JSON.stringify(this.addForm))
            editForm.last_ip = sessionStorage.getItem("Ip")
            editForm.userId = localStorage.getItem("ID")
            editEyesConfig(editForm).then(res => {
              this.$message.success(res.msg)
              this.formVisible = false
              this.getEyesConfigTableData()
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      delEyes(id) {
        this.deleteDialogs.show = true
        this.deleteDialogs.deleteId = id
      },
      //删除按钮事件
      deleteEvents() {
        let params = {
          userId: localStorage.getItem("ID"),
          last_ip: sessionStorage.getItem("Ip"),
          id: this.deleteDialogs.deleteId,
        }
        delEyesConfig(params).then(res => {
          if (res.code == 200) {
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
          this.getEyesConfigTableData()
        }).catch(err => {
          console.log("请求失败", err)
        })
      },
      //分页
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getEyesConfigTableData()
      },
      handleCurrentChange(val) {
        this.listQuery.startSize = val
        this.getEyesConfigTableData()
      },
      //下拉动态table子组件
      alterCheckbok(key, conte) {
        this.key = key;
        this.colData.filter(i => {
          if (conte.indexOf(i.title) != -1) {
            i.istrue = false;
            this.$nextTick(() => {
              this.$refs.tableDataRef.doLayout();
            });
          } else {
            i.istrue = true;
            this.$nextTick(() => {
              this.$refs.tableDataRef.doLayout();
            });
          }
        });
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.editSubjects = false
        this.$nextTick(() => {
          this.addForm = this.$options.data().addForm
        })
      },
    }
  }
</script>



